﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBSourceToolsLib.Suggestions;
using DBSourceToolsLib.Composite;

namespace ICSharpCode.MSSQLExtensions.SqlParser
{
    public class SenseBuilder
    {
        private ICSharpCode.TextEditor.Document.IDocument iDocument;
        private TextWordParser m_TextWordParser = new TextWordParser();
        private DBSourceToolsLib.Composite.IDBComposite m_DBComposite = null;

        public SenseBuilder(ICSharpCode.TextEditor.Document.IDocument iDocument)
        {
            this.iDocument = iDocument;
        }

        public void SetTableNameList(List<string> list)
        {
            m_TextWordParser.SetTableNameList(list);
        }

        public void SetFieldNameList(List<string> list)
        {
            m_TextWordParser.SetFieldNameList(list);
        }

        public void SetViewNameList(List<string> list)
        {
            m_TextWordParser.SetViewNameList(list);
        }

        public List<Suggestion> GetSuggestionsForCursorAt(int iCursorOffset)
        {
            m_TextWordParser.ClearWords();

            BuildWordList();
            List<Suggestion> oList = new List<Suggestion>();

            if (!m_TextWordParser.IsAfterWord(iCursorOffset))
                return oList;

            if (m_TextWordParser.IsAfterField(iCursorOffset))
                return oList;

            if (m_TextWordParser.IsAfterTableOrAlias(iCursorOffset))
                return oList;

            if (m_TextWordParser.IsAfterFrom(iCursorOffset))
            {
                return m_DBComposite.BuildList(FieldListType.Long, DBCompositeType.Table, DBCompositeType.View);
            }

            LinkedListNode<SqlWord> oSelectNode = null;
            if (m_TextWordParser.IsInSelect(iCursorOffset, out oSelectNode))
            {
                return m_DBComposite.BuildFieldList(m_TextWordParser.FindTablesAndAliases(oSelectNode));
            }

            if (oList.Count() == 0)
                oList.Add(new Suggestion { ImageIdx = 0, Name = "*", Description = "" });
            return oList;
        }

        public void BuildWordList()
        {
            foreach (ICSharpCode.TextEditor.Document.LineSegment oLineSegment in iDocument.LineSegmentCollection)
                foreach (ICSharpCode.TextEditor.Document.TextWord oWord in oLineSegment.Words)
                    if (!oWord.IsWhiteSpace)
                    {
                        m_TextWordParser.AddTextWord(oWord, oLineSegment.Offset);
                        //        HCSocketToolsLib.HCSocketSender.Instance().SendUDPMessage(
                        //System.Reflection.MethodBase.GetCurrentMethod()
                        //, oWord.Word);

                    }
        }


        public void SetComposite(DBSourceToolsLib.Composite.IDBComposite oComposite)
        {
            m_DBComposite = oComposite;
            SetTableNameList(SuggestionListToSimple(m_DBComposite.BuildList(FieldListType.Long, DBCompositeType.Table)));
            SetFieldNameList(SuggestionListToSimple(m_DBComposite.BuildList(FieldListType.Short, DBCompositeType.Field)));
            SetViewNameList(SuggestionListToSimple(m_DBComposite.BuildList(FieldListType.Long, DBCompositeType.View)));
        }

        public static List<string> SuggestionListToSimple(List<Suggestion> oListSuggestion)
        {
            return oListSuggestion.Select(sugg => sugg.Name).ToList();
        }

    }
}
